package com.hzlj.position.locate.differentiation.sysCheck.traceCheck;

import cn.hutool.core.util.ObjectUtil;
import com.hzlj.common.message.common.dto.message.MessageNoticeResultDTO;
import com.hzlj.position.config.common.enums.ReportType;
import com.hzlj.position.config.common.enums.WarningType;
import com.hzlj.position.locate.common.common.PositionSimpleDTO;
import com.hzlj.position.locate.common.dto.position.PositionQueryResultDTO;
import com.hzlj.position.locate.common.dto.reportInspection.ReportInspectionExtDTO;
import com.hzlj.position.locate.common.enums.PositionSysCheck;
import com.hzlj.position.locate.common.model.Position;
import com.hzlj.position.locate.differentiation.sysCheck.traceCheck.cross.CrossSysCheck;
import com.hzlj.position.locate.differentiation.sysCheck.traceCheck.demount.DemountSysCheck;
import com.hzlj.position.locate.service.PositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 位置确认路由
 */
@Slf4j
@Service
public class TraceSysCheckRouter {
    @Resource
    private CrossSysCheck crossSysCheck;
    @Resource
    private DemountSysCheck demountSysCheck;
    @Resource
    private PositionService positionService;

    /**
     * 第一步：通知矫正对象上报
     */
    public void noticeJzry(List<Position> positions,
                           Map<String, MessageNoticeResultDTO> notices) {
        //按照类型分组
        Map<String, List<Position>> positionsByWarningType = positions.stream().collect(Collectors.groupingBy(Position::getWarningType));
        for (Map.Entry<String, List<Position>> entry : positionsByWarningType.entrySet()) {
            switch (WarningType.of(entry.getKey())) {
                case CROSS:
                    crossSysCheck.noticeJzry(entry.getValue(), notices);
                    break;
                case DEMOUNT:
                    demountSysCheck.noticeJzry(entry.getValue(), notices);
                    break;
            }
        }
    }


    /**
     * 第二步：矫正人员上报
     *
     * @param report 上报位置
     * @param ext    原始记录
     */
    public void updateReportResult(String jzId,
                                   String jgCode,
                                   PositionSimpleDTO report,
                                   ReportInspectionExtDTO ext) {
        PositionQueryResultDTO position = positionService.getPosition(ext.getPositionId(), ext.getPositionTime());

        switch (WarningType.of(position.getWarningType())) {
            case CROSS:
            case CROSS_CHECK:
                crossSysCheck.updateReportResult(jzId, jgCode, report, PositionSimpleDTO.from(position));
                break;
            case DEMOUNT:
            case DEMOUNT_CHECK:
                demountSysCheck.updateReportResult(jzId, jgCode, report, PositionSimpleDTO.from(position));
                break;
        }
    }

    public String getType(String reportType,
                          ReportInspectionExtDTO ext) {
        if (ext == null
                || ObjectUtil.isEmpty(ext.getPositionId())
                || ObjectUtil.isEmpty(ext.getPositionTime())
                || !ReportType.TRACK_CHECK.getCode().equals(reportType)) {
            return null;
        }

        PositionQueryResultDTO position = positionService.getPosition(ext.getPositionId(), ext.getPositionTime());
        if (!ObjectUtil.equal(position.getSysCheck(), PositionSysCheck.ING.getCheck())) {
            log.warn("[位置确认]已经有系统核查结果，不需要重复核查:originPositionId={}", ext.getPositionId());
            return null;
        }
        switch (WarningType.of(position.getWarningType())) {
            case CROSS:
            case CROSS_CHECK:
                return crossSysCheck.getType().getDataType();
            case DEMOUNT:
            case DEMOUNT_CHECK:
                return demountSysCheck.getType().getDataType();
            default:
                return null;
        }
    }


}
